package com.dreamwhale.accelerator.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dreamwhale.accelerator.common.DataResult;
import com.dreamwhale.accelerator.entity.*;
import com.dreamwhale.accelerator.mapper.*;
import com.dreamwhale.accelerator.service.AlUserService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author Author
 * @since 2020-08-07
 */
@RestController
@RequestMapping("/user")
public class AlUserController {

     @Resource
    private AlUserService alUserService;

     @Resource
     private AlUserMapper alUserMapper;

     @Resource
     private AlWithdrawalRulesMapper alWithdrawalRulesMapper;

     @Resource
     private AlUserWithdrawMapper alUserWithdrawMapper;

     @Resource
     private AlUserFinancialRecordsMapper alUserFinancialRecordsMapper;

     @Resource
     private AlAgentRuleMapper alAgentRuleMapper;

     @Resource
     private AlVipMapper alVipMapper;
    /**
     * 查询我的信息
     * @return
     */
    @RequestMapping("/selectAlUserInfo")
    public DataResult selectAlUserInfo(String userId) {
        return DataResult.ok(alUserService.selectAlUserInfo(userId));
    }

    /**
     * 提现
     *
     * @param
     * @return
     */
    @RequestMapping("/insertALUserWithdraws")
    @Transactional
    public DataResult insertALUserWithdraws(AlUserWithdraw alUserWithdraw) {
        //查询余额
        AlUser alUser = alUserMapper.selectById(alUserWithdraw.getUserId());
        //手续费
        QueryWrapper<AlWithdrawalRules> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(AlWithdrawalRules::getWrDel,'0');
        AlWithdrawalRules alWithdrawalRules=alWithdrawalRulesMapper.selectOne(queryWrapper);
        //手续费
        BigDecimal Shou=BigDecimal.valueOf(alWithdrawalRules.getWrCharge()).divide(BigDecimal.valueOf(100));
        //提现+手续费
        BigDecimal prices=alUserWithdraw.getUwMoney().add(Shou);
        if (alUser.getUserMoney().compareTo(prices) >= 0) {
            //插入提现申请
            alUserWithdraw.setUwCreatTime(LocalDateTime.now());
            alUserWithdrawMapper.insert(alUserWithdraw);
            //修改钱包
            alUser.setUserMoney(alUser.getUserMoney().subtract(prices));
            alUserMapper.updateById(alUser);
            //插入收支记录
            AlUserFinancialRecords alUserFinancialRecords=new AlUserFinancialRecords();
             alUserFinancialRecords.setUserId(alUserWithdraw.getUserId());
             alUserFinancialRecords.setUfrMoney(prices);
             alUserFinancialRecords.setUfrNowMoney(alUser.getUserMoney());
             alUserFinancialRecords.setUfrType(3);//1.返佣2.充值3.提现4.购买会员
            alUserFinancialRecords.setUfrStatus(1);//支出
            alUserFinancialRecords.setUfrCreateTime(LocalDateTime.now());
            alUserFinancialRecordsMapper.insert(alUserFinancialRecords);
            System.out.println(alUserWithdraw.getUwName());
            System.out.println(alUserWithdraw.getUwRealName());
            return DataResult.ok("1");
        }
        return DataResult.build(300, "余额不足");
    }


    /**
     * 我的-推荐
     * @return
     */
    @RequestMapping("/selectUserInvitatInfo")
    public DataResult selectUserInvitatInfo(String userId) {
        HashMap<Object, Object> map = new HashMap<>();
      //查询推荐人数
        AlUser alUser=alUserMapper.selectById(userId);
        //查询等级
        if(alUser.getUserAgentLeve()!=0){
            QueryWrapper<AlAgentRule> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda().eq(AlAgentRule::getUarLevel,alUser.getUserAgentLeve());
            AlAgentRule alAgentRule=alAgentRuleMapper.selectOne(queryWrapper);
            //距离上级人数
            QueryWrapper<AlAgentRule> queryWrappers = new QueryWrapper<>();
            queryWrappers.gt("uar_number",alUser.getUserRecommend()).orderByAsc("uar_number");
            AlAgentRule alAgentRules=alAgentRuleMapper.selectList(queryWrappers).get(0);
            Integer num=alAgentRules.getUarNumber()-alUser.getUserRecommend();
            //推荐人数
            map.put("recommend",alUser.getUserRecommend());
            //当前代理等级
            map.put("agentName",alAgentRule.getUarName());
            //距离升级人数
            map.put("upgrade",num);
            return DataResult.ok(map);
        }else{
            //距离上级人数
            QueryWrapper<AlAgentRule> queryWrappers = new QueryWrapper<>();
            queryWrappers.gt("uar_number",alUser.getUserRecommend()).orderByAsc("uar_number");
            AlAgentRule alAgentRules=alAgentRuleMapper.selectList(queryWrappers).get(0);
            Integer num=alAgentRules.getUarNumber()-alUser.getUserRecommend();
            //推荐人数
            map.put("recommend",alUser.getUserRecommend());
            //当前代理等级
            map.put("agentName",0);
            //距离升级人数
            map.put("upgrade",num);
            return DataResult.ok(map);
        }


    }

    /**
     * 会员
     * @return
     */
    @RequestMapping("/selectUserUseVipInfo")
    public DataResult selectUserUseVipInfo(String userId) {
        HashMap<Object, Object> map = new HashMap<>();
        AlUser alUser=alUserMapper.selectUseTimes(userId);
        map.put("alUser",alUser);
        QueryWrapper<AlVip> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(AlVip::getUvDel,'0');
        List<AlVip> alVip=alVipMapper.selectList(queryWrapper);
        map.put("alVip",alVip);
        return DataResult.ok(map);

    }

}

